from django.shortcuts import render, redirect
import datetime

# Create your views here.
def index(request):
    return render(request,'index.html')

def password(request):
    if request.method == 'GET':
        #判断是否已经登录过了
        if 'password' in request.session:
            return redirect('/release')
        return render(request, 'password.html')
    else:
        pwd = request.POST['password']
        if pwd == 'lying520' or pwd == 'xiaolin1314':
            request.session['password'] = pwd
            return redirect('/release')
        else:
            dic = {'err':'密码错误'}
            return render(request, 'password.html', dic)

def release(request):
    if request.method == 'GET':
        # 先判断session中有没有信息
        if 'password' in request.session:
            if request.session['password'] == 'xiaolin1314':
                user = 'lgg'
            else:
                user = 'dxy'
            return render(request, 'release.html', locals()) 
        else:
            return redirect('/password')
    else:
        # 接收表单数据
        date = request.POST['date']
        if not date:
            date = datetime.datetime.now().strftime("%Y-%m-%d")
        type_num = request.POST['type_num']
        color_num = request.POST['color_num']
        text = request.POST['text']
        # 讲数据存入数据库

        return render(request, 'release.html') 